package com.example.teamtribe.main.chat.chatting;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.database.Chatting;
import com.example.teamtribe.database.ChattingBox;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

public class ChattingAdapter extends RecyclerView.Adapter<ChattingAdapter.ViewHolder> {

    private Context context;
    private ChattingBox chattingBox;

    public ChattingAdapter(ChattingBox chattingBox) {
        this.chattingBox = chattingBox;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_message, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Chatting chatting = chattingBox.getChattingList().get(position);
        User user = Utility.getT(chatting.getUserId(), User.class);
        if (user.getRealId() == Utility.getUser().getRealId()) {
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.rightName.setText(user.getNickname());
            holder.rightMsg.setText(chatting.getContend());
            Glide.with(context).load(Utility.getUri(user.getPortrait())).into(holder.rightHead);
        } else {
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.leftName.setText(user.getNickname());
            holder.leftMsg.setText(chatting.getContend());
            Glide.with(context).load(Utility.getUri((user.getPortrait()))).into(holder.leftHead);
        }
    }

    @Override
    public int getItemCount() {
        return chattingBox.getChattingList().size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout leftLayout;
        private RelativeLayout rightLayout;
        private TextView leftMsg;
        private TextView rightMsg;
        private ImageView leftHead;
        private ImageView rightHead;
        private TextView leftName;
        private TextView rightName;

        public ViewHolder(View itemView) {
            super(itemView);
            leftLayout = itemView.findViewById(R.id.left_layout);
            rightLayout = itemView.findViewById(R.id.right_layout);
            leftMsg = itemView.findViewById(R.id.left_msg);
            rightMsg = itemView.findViewById(R.id.right_msg);
            leftHead = itemView.findViewById(R.id.left_head);
            rightHead = itemView.findViewById(R.id.right_head);
            leftName = itemView.findViewById(R.id.left_name);
            rightName = itemView.findViewById(R.id.right_name);
        }
    }
}
